#include "regist.h"
#include "ui_regist.h"

extern QTcpSocket *s;

regist::regist(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::regist)
{
    ui->setupUi(this);

    flag = false;

    this->setAttribute(Qt::WA_DeleteOnClose);

    QFile file(":/login.qss"); // 假设style.qss位于资源文件中
    if (file.open(QFile::ReadOnly))
    {
        setStyleSheet(QString(file.readAll())); // 应用全局样式
        file.close();
    }

    setWindowFlag(Qt::FramelessWindowHint,true);//去掉主窗口的边框
    setAttribute(Qt::WA_TranslucentBackground,true);//把主窗口透明

    connect(s, &QTcpSocket::readyRead, this, [=]{
        QString str = s->readAll();
        qDebug()<<str;
        if("exist" == str)
        {
            flag = false;
            QToolTip::showText(ui->usernameLineEdit->mapToGlobal(QPoint(25, 15)), "账号已存在，请重新输入账号！");
        }
        else if("no exist" == str)
            flag = true;
    });
    connect(s, &QTcpSocket::disconnected, this, [=]{

    });
    s->connectToHost("192.168.32.188",9000);
}


regist::~regist()
{
    s->disconnectFromHost();
    qDebug()<<"regist";
    delete ui;
}


void regist::clear()
{
    ui->usernameLineEdit->clear();
    ui->passwordLineEdit->clear();
    ui->rePasswordLineEdit->clear();
}

void regist::on_loginButton_clicked()
{
    if(true == flag)
    {
        if(false == ui->usernameLineEdit->text().contains(' '))
        {
            if(false == ui->passwordLineEdit->text().contains(' '))
            {
                if("" != ui->usernameLineEdit->text() && "" != ui->passwordLineEdit->text())
                {
                    if(ui->passwordLineEdit->text() == ui->rePasswordLineEdit->text())
                    {
                        QString msg = QString("regist") + ' ';
                        msg += ui->usernameLineEdit->text() + ' ' + ui->passwordLineEdit->text();
                        qDebug()<<msg;
                        s->write(msg.toLocal8Bit());
                    }
                    else
                    {
                        QToolTip::showText(ui->rePasswordLineEdit->mapToGlobal(QPoint(25, 15)), "两次密码输入不一致，请重新进行输入！");
                    }
                }
                else if("" == ui->passwordLineEdit->text())
                {
                    QToolTip::showText(ui->passwordLineEdit->mapToGlobal(QPoint(25, 15)), "密码不允许为空，请输入密码！");
                }
                else if("" == ui->usernameLineEdit->text())
                {
                    QToolTip::showText(ui->usernameLineEdit->mapToGlobal(QPoint(25, 15)), "用户名不允许为空，请输入用户名！");
                }

            }
            else
                QToolTip::showText(ui->passwordLineEdit->mapToGlobal(QPoint(25, 15)), "密码不允许包含空格，请重新进行输入！");
        }
        else
            QToolTip::showText(ui->usernameLineEdit->mapToGlobal(QPoint(25, 15)), "用户名不允许包含空格，请重新进行输入！");
    }
    else
        QToolTip::showText(ui->usernameLineEdit->mapToGlobal(QPoint(25, 15)), "账号已存在，请重新输入账号！");
}


void regist::on_closeButton_clicked()
{
    clear();
    this->close();
}

void regist::mousePressEvent(QMouseEvent *event)
{
    if(false == ui->loginButton->underMouse() && false == ui->closeButton->underMouse())
        mouseOff = event->globalPos() - geometry().topLeft();
    ui->passwordLineEdit->clearFocus();
    ui->usernameLineEdit->clearFocus();
}

void regist::mouseMoveEvent(QMouseEvent *event)
{
    if(false == ui->loginButton->underMouse() && false == ui->closeButton->underMouse())
        this->move(event->globalPos() - mouseOff);
}

void regist::on_usernameLineEdit_textChanged(const QString &arg1) //编辑框变动槽函数
{
    qDebug()<<arg1;
    s->write(arg1.toLocal8Bit());
}

